package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.utils.bean.BeanUtils;

import com.pureut.quality.domain.CustomerManagement;
import com.pureut.quality.domain.OrderSupplierManagement;
import com.pureut.quality.domain.dto.OrderSupplierManagementMqDto;
import com.pureut.quality.mapper.OrderSupplierManagementMapper;
import com.pureut.quality.service.OrderSupplierManagementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/***
 *
 * Author:C
 * Date:2023/01/19 17:12
 * @DESC
 */
@Service
public class OrderSupplierManagementServiceImpl extends ServiceImpl<OrderSupplierManagementMapper, OrderSupplierManagement> implements OrderSupplierManagementService {

    @Resource
    OrderSupplierManagementMapper orderSupplierManagementMapper;

    /**
     * 新增
     *
     * @param orderSupplierManagementMqDto
     * @return
     */
    @Override
    public boolean insertOrderSupplierManagement(OrderSupplierManagementMqDto orderSupplierManagementMqDto) {
        if (orderSupplierManagementMqDto.getSupplierNum() != null) {
            OrderSupplierManagement orderSupplierManagement1 = orderSupplierManagementMapper.selectOne(new QueryWrapper<OrderSupplierManagement>().lambda().eq(OrderSupplierManagement::getSupplierNum, orderSupplierManagementMqDto.getSupplierNum()));
            if (orderSupplierManagement1 == null) {
                OrderSupplierManagement orderSupplierManagement = new OrderSupplierManagement();
                BeanUtils.copyProperties(orderSupplierManagementMqDto, orderSupplierManagement);
                return save(orderSupplierManagement);
            }
        }
        return false;
    }

    /**
     * 修改
     *
     * @param orderSupplierManagementMqDto
     * @return
     */
    @Override
    public boolean updateOrderSupplierManagement(OrderSupplierManagementMqDto orderSupplierManagementMqDto) {
        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectOne(new QueryWrapper<OrderSupplierManagement>().lambda().eq(OrderSupplierManagement::getSupplierNum, orderSupplierManagementMqDto.getSupplierNum()));

        BeanUtils.copyProperties(orderSupplierManagementMqDto, orderSupplierManagement);


        return updateById(orderSupplierManagement);
    }

    /**
     * 删除
     *
     * @param orderSupplierManagementMqDto
     */
    @Override
    public void deleteOrderSupplierManagement(OrderSupplierManagementMqDto orderSupplierManagementMqDto) {
        if (orderSupplierManagementMqDto.getSupplierNum() != null) {
            List<String> codes = Arrays.asList(orderSupplierManagementMqDto.getSupplierNum().split(","));
            List<OrderSupplierManagement> orderSupplierManagements = orderSupplierManagementMapper.selectList(new QueryWrapper<OrderSupplierManagement>().lambda().in(OrderSupplierManagement::getSupplierNum, codes));
            if (orderSupplierManagements.size() > 0) {
                removeByIds(orderSupplierManagements);
            }
        }
    }
}
